const request = require('./utils/request')
import {
  CustomRouteType
} from '../../utils/custom-route/index'

Page({
  data: {
    devName: '',
    devMac: '',
    floorInfoArr: [],
    areaInfoArr: [],
    areaInfoIndex: '',
    floorInfoIndex: ''
  },
  onLoad(options) {
    console.log(options);
    // 开始获取。0 是区域信息，1 是楼层信息
    this.getAreaAndFloorInfo(0)
    this.getAreaAndFloorInfo(1)

    // 获取保存的服务器信息
    this.getServiceInfoIsOK()
    //  处理数据
    this.devName = options.devName
    this.devMac = options.devMac
    this.setData({
      devName: this.devName,
      devMac: this.devMac
    })
  },
  formSubmit(e) {
    console.log(this.data.areaInfoArr);
    console.log(this.data.floorInfoArr);
    let _this = this
    if (this.floorInfoIsEmpty(e.detail.value) && this.getServiceInfoIsOK()) {
      let info = e.detail.value
      // 拿到区域和楼层的索引
      let areaInfoIndex = Number(info.areaInfo)
      let floorInfoIndex = Number(info.floorInfo)
      // 利用索引去查询信息
      let areaName = this.data.areaInfoArr[areaInfoIndex]
      let floorName = this.data.floorInfoArr[floorInfoIndex]
      info.areaInfo = areaName
      info.floorInfo = floorName
      console.log('form发生了submit事件，携带数据为：', info)
    }
  },

  floorInfoIsEmpty(info) {
    if (!info.areaInfo) {
      return this.errorResult('请选择区域信息')
    }
    if (!info.floorInfo) {
      return this.errorResult('请选择楼层信息')
    }
    if (!info.roomInfo) {
      return this.errorResult('房间信息为空')
    }
    if (!info.devMac || info.devMac === 'undefined') {
      return this.errorResult('设备地址为空')
    }
    if (!info.devName || info.devName === 'undefined') {
      return this.errorResult('设备名称为空')
    }
    return true
  },

  errorResult(errorInfo) {
    wx.showToast({
      title: errorInfo,
      icon: 'error',
      duration: 2000
    })
    return false
  },

  successResult(successInfo) {
    wx.showToast({
      title: successInfo,
      icon: 'success',
      duration: 2000
    })
    return true
  },

  setServiceInfo() {
    let _this = this
    wx.showModal({
      title: '提示',
      content: '你没有配置服务器信息，是否前往？',
      success(res) {
        if (res.confirm) {
          _this.saveServiceInfo()
        } else if (res.cancel) {
          _this.back()
        }
      }
    })
  },

  saveServiceInfo() {
    wx.navigateTo({
      url: '/pages/save-service-info/index',
      routeType: CustomRouteType.HalfScreenDialog
    });
  },

  back() {
    wx.navigateBack({
      delta: 1,
    });
  },

  getServiceInfoIsOK() {
    let serviceInfo = wx.getStorageSync('serviceInfo')
    if (!serviceInfo) {
      this.setServiceInfo()
    }
    return serviceInfo
  },

  getAreaAndFloorInfo(data) {
    let num = Number(data)
    request.post(num).then(res => {
      let arr = []
      if (num === 0) {
        this.data.areaInfoArr = res.data
        this.data.areaInfoArr.forEach(item => {
          arr.push(item)
        })
        this.setData({
          areaInfoArr: arr
        })
      } else {
        this.data.floorInfoArr = res.data
        this.data.floorInfoArr.forEach(item => {
          arr.push(item)
        })
        this.setData({
          floorInfoArr: arr
        })
      }
    })
  },

  bindPickerChangeForArea(e) {
    this.data.areaInfoIndex = e.detail.value
    this.setData({
      areaInfoIndex: this.data.areaInfoIndex
    })
  },
  
  bindPickerChangeForFloor(e) {
    this.data.floorInfoIndex = e.detail.value
    this.setData({
      floorInfoIndex: this.data.floorInfoIndex
    })
  }
})